﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for PlaneBusiness
/// </summary>
public class PlaneBusiness
{
	public PlaneBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(PlaneEntities item)
    {
        bool flat = false;
        string[] para = { "@planeSymbol", "@planeName", "@planeSeat", "@planeInfo" };
        string[] values = { item._planeSymbol, item._planeName, item._planeSeat.ToString(), item._planeInfo };
        if (DataAccess.getExecProcedure("sp_InsertPlane", para, values) > 0)
        {
            flat = true;
        }
        else
            flat = false;
        return flat;
    }

    public bool Edit(PlaneEntities item)
    {
        bool flat = false;
        string[] para = { "@planeID", "@planeSymbol", "@planeName", "@planeSeat", "@planeInfo" };
        string[] values = { item._planeID.ToString(),item._planeSymbol,item._planeName,item._planeSeat.ToString(),item._planeInfo};
        if (DataAccess.getExecProcedure("sp_UpdatePlane", para, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Delete(int id)
    {
        if (DataAccess.getExec("sp_DeletePlane '"+id+"'") > 0)
            return true;
        else
            return false;
    }

    public DataTable getAllPlane()
    {
        return DataAccess.DataTable_Query("exec sp_GetPlane '0'");
   }

    public PlaneEntities getPlane(int planeID)
    {
        DataTable dt = DataAccess.DataTable_Query("exec sp_GetPlane '" + planeID + "'");
        PlaneEntities pe = new PlaneEntities();
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            pe._planeID = int.Parse(dr["planeID"].ToString());
            pe._planeSymbol = dr["planeSymbol"].ToString();
            pe._planeName = dr["planeName"].ToString();
            pe._planeSeat = int.Parse(dr["planeSeat"].ToString());
            pe._planeInfo = dr["planeInfo"].ToString();
        }
        return pe;
    }

    public PlaneEntities getSeatOfPlane(int planeID,int classID)
    {
        DataTable dt= DataAccess.DataTable_Query("exec sp_GetSeatOfClass '"+planeID+"','"+classID+"'");
        PlaneEntities pe = new PlaneEntities();
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            pe._planeSeat = int.Parse(dr["amount"].ToString());
        }
        return pe;
    }

    public DataTable getPlaneBy(string[] TableName, string[] fields, string Criterias, string priorities)
    {
        DataTable dt = new DataTable();
        string query = SQLQuery.SQLQuery_SELECT(TableName, fields, Criterias, priorities);
        return dt = DataAccess.DataTable_Query(query);
    }
}